package com.platform.task;

import com.platform.common.utils.netty.ChannelSupervise;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.module.domain.BaseConcentratorData;
import com.platform.module.service.IBaseConcentratorDataService;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 采集器定时调度任务
 *
 * @author platform
 */
@Component("concentratorTask")
public class ConcentratorTask {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IBaseConcentratorDataService concentratorDataService;

    /**
     * 根据采集器id采集数据
     * @param concentratorIds
     */
    public void concentratorTaskById(String concentratorIds) {
        for(String concentratorId : concentratorIds.split(",")){
            //获取采集器高位
            String cenhigh = concentratorId.substring(0,2);
            //获取采集器低位
            String cenlow = concentratorId.substring(2,4);
            //判断通道是否存在
            if(!redisUtil.hHasKey("sys_netty", cenhigh+cenlow)){
                break;
            }
            //获取通道id
            String channelId = redisUtil.hget("sys-netty",cenhigh+cenlow).toString();
            Channel channel = ChannelSupervise.findChannel(channelId);
            if(channel==null){
                break;
            }
            //控制码
            String cont = "05";
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(cont)+NettyUtil.get10HexNum("09");
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            String response = "AA "+ cenlow +" "+ cenhigh + " 00 00 00 00 " + cont + " 00 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            NettyUtil.writeToClient(revertStr,channel,concentratorId+"全部设备信息采集");
        }
    }

    /**
     * 采集所有采集器信息
     * @param
     */
    public void concentratorTask() {
        List<BaseConcentratorData> list = concentratorDataService.list();
        for(BaseConcentratorData concentratorData : list){
            //获取采集器高位
            String cenhigh = concentratorData.getConcentratorId().substring(0,2);
            //获取采集器低位
            String cenlow = concentratorData.getConcentratorId().substring(2,4);
            //判断通道是否存在
            if(!redisUtil.hHasKey("sys_netty", cenhigh+cenlow)){
                break;
            }
            //获取通道id
            String channelId = redisUtil.hget("sys-netty",cenhigh+cenlow).toString();
            Channel channel = ChannelSupervise.findChannel(channelId);
            if(channel==null){
                break;
            }
            //控制码
            String cont = "05";
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(cont);
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            String response = "AA "+ cenlow +" "+ cenhigh + " 00 00 00 00 " + cont + " 00 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            NettyUtil.writeToClient(revertStr,channel,concentratorData.getConcentratorId()+"全部设备信息采集");
        }
    }
}
